<?php
if ($this->Session->flash()):
?>
	<div class="alert alert-warning">
		<?php  echo $this->Session->flash();?>
	</div>
<?php
endif;
?>

<div class="row">
	<div class="col-lg-12">
		<h1 class="page-header">Tạo link</h2>
	</div>
</div>
<?php echo $this->Form->create('CreateLink', array('class' => 'ajax_page', 'name' => 'create_link'));?>
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
	<tr>
		<th width="20%">Chọn trang</th>
		<td>
			<select class="form-control" onchange="change_file(this.value)">
				<option value="/">Trang chủ</option>
				<option value="/lien-he.html">Trang liên hệ</option>
				<option value="/ngan-hang-24h/dang-ky-vay-online.html">Đăng ký vay online</option>
			</select>
		</td>
	</tr>
	<tr>
		<th>Danh mục</th>
		<td>
			<select id="change_category" name="iCat" class="form-control">
				<option value="0">Chọn category</option>
				<?php
				$cat_type = "";
				foreach ($datas as $cat):
				?>
					<?php
					if($cat_type != $cat["cat_type"]):
						$cat_type = $cat["cat_type"];
					?>
						<optgroup label="<?php echo $category_type[$cat_type];?>"></optgroup>
					<?php
					endif;
					?>
					<option value="<?php echo $cat['id'];?>">
					<?php for($j = 0; $j < $cat["level"]; $j++) echo "---";?>
						<?php echo $cat['cat_name'];?>
					</option>
				<?php endforeach;?>
			</select>
			<div class="hidden">
				<?php
				foreach ($datas as $cat):
				?>
					<div id="category<?php echo $cat['id'];?>">
						<?php
						$link = $this->Html->url(
								array(
									'controller' => 'categorys',
									'action' => 'view',
									//'full_base' => false,
									//'cat_id' => $cat['id'],
									'cat_rewrite' => $cat['cat_rewrite'],
									//'ext' => 'html',
								)
						);
						$link = preg_replace('#/admin/#', '/', $link);
						echo $link;
// 						echo '/danh-muc-' . $cat['id'] . '/' . $cat['cat_rewrite'] . '.html';
						?>
					</div>
				<?php endforeach;?>
			</div>
		</td>
	</tr>

	<tr>
		<th>Link</th>
		<td>
			<div id="link_category"></div>
		</td>
	</tr>
	<tr>
		<th>&nbsp;</th>
		<td>
			<div class="form-group">
			<?php
			echo $this->Form->hidden('cat_active', array('value' => 1));

			echo $this->Form->button('Create link', array(
					'type' => 'submit',
					'name' => 'create_link_category',
					'id' => 'create_link_category',
					'class' => 'btn btn-primary',
					'onclick' => 'link_to_category()',
					'label' => false,
					'div' => false,
				)
			);
			?>
			</div>
		</td>
	</tr>
</table>
<?php echo $this->Form->end()?>
<div class="response-ajax-create-link"></div>
<script type="text/javascript">

	function change_file(filename){
		//document.getElementById("link_category").value = filename;
		document.getElementById("link_category").innerHTML = filename;
	}
	function link_to_category(){
		window.opener.document.getElementById("<?php echo $id;?>").value = window.document.getElementById("link_category").innerHTML;
		window.close();
	}

	function link_to_data(str_link){
		window.opener.document.getElementById("<?php echo $id;?>").value = str_link;
		window.close();
	}
</script>
